alias cv="cd /d/javaEnv/vagrant_bigdata_cluster"
alias gitup='sed -i "/# GitHub520 Host Start/Q" /c/Windows/System32/drivers/etc/hosts && curl https://raw.hellogithub.com/hosts >> /c/Windows/System32/drivers/etc/hosts'
alias vup="vagrant up"
alias vdown="vagrant halt"
alias vreload="vagrant reload --provision"
alias getlogin="python /c/Users/cuiyufei/getLoginInfo.py"
alias clearauto="sed -i '/"session.autoLogin"/d' /d/Portable/WindTerm_2.5.0/.wind/profiles/default.v10/terminal/user.sessions"
HOSTS_PATH=/c/Windows/System32/drivers/etc/hosts
SESSION_PATH=/d/Portable/WindTerm_2.5.0/.wind/profiles/default.v10/terminal/user.sessions
DATA_PATH=/c/host_info.txt
PYTHON_FILE=/c/getLoginInfo.py

setclear() {
sed -i '/"session.autoLogin"/d' ${SESSION_PATH}
sed -i "/# Qingjiao Host Start/Q" ${HOSTS_PATH}
}

get_item() {
local type=$1
local path=${2:-$DATA_PATH}
if [ -f ${path} ];then
    ret=`cat ${path} |grep ${type} |awk -F '=' '{print $2}'`
    echo ${ret}
else
    echo "$path not exists!"
fi
}

clusterip() {
local master=$1
local slave1=$2
local slave2=$3
usage="Usage: clusterip master_external_ip slave1_external_ip slave2_external_ip"
if [ $# -ne 3 ]; then
    echo $usage
    exit 1
fi
if [ `cat /c/Windows/System32/drivers/etc/hosts|grep master|wc -l` != 0 ];then
    sed -i '/master/d' ${HOSTS_PATH}
    sed -i '/slave1/d' ${HOSTS_PATH}
    sed -i '/slave2/d' ${HOSTS_PATH}
fi
echo "$master master" >> ${HOSTS_PATH}
echo "$slave1 slave1" >> ${HOSTS_PATH}
echo "$slave2 slave2" >> ${HOSTS_PATH}
}

singleip() {
usage="Usage: singleip hadoop000_external_ip"
if [ $# -ne 1 ]; then
    echo $usage
    exit 1
fi
local hadoop000=$1
if [ `cat /c/Windows/System32/drivers/etc/hosts|grep master|wc -l` != 0 ];then
    sed -i '/hadoop000/d' ${HOSTS_PATH}
fi
echo "$hadoop000 hadoop000" >> ${HOSTS_PATH}
}

jupyterip() {
usage="Usage: jupyterip jupyter_external_ip"
if [ $# -ne 1 ]; then
    echo $usage
    exit 1
fi
local jupyter=$1
if [ `cat /c/Windows/System32/drivers/etc/hosts|grep jupyter|wc -l` != 0 ];then
    sed -i '/jupyter/d' ${HOSTS_PATH}
fi
echo "$jupyter jupyter" >> ${HOSTS_PATH}
}

set_host_ip() {
local host_name=$1
local host_ip=$2
usage="Usage: set_host_ip host_name external_ip"
if [ $# -ne 2 ]; then
    echo $usage
    exit 1
fi

if [ `cat /c/Windows/System32/drivers/etc/hosts |grep ${host_name} |wc -l` != 0 ];then
    sed -i "/${host_name}/d" ${HOSTS_PATH}
fi
echo "$host_ip $host_name" >> ${HOSTS_PATH}
}

setvar() {
python ${PYTHON_FILE}
echo "# Qingjiao Host Start" >> ${HOSTS_PATH}
HOSTNAME_LIST=("master" "slave1" "slave2" "Hadoop/Hive/Spark" "jupyter")
for host in ${HOSTNAME_LIST[@]};do
    if [ `cat ${DATA_PATH} |grep ${host} |wc -l` != 0 ];then
        if [ "${host}" == "Hadoop/Hive/Spark" ];then
            set_host_ip hadoop000 `get_item ${host}_public_ip`
        else
            set_host_ip ${host} `get_item ${host}_public_ip`
        fi
    fi
done
}
